﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.DesktopClient.Model;
using EzDesk.Clients.Utility;

namespace EzDesk.DesktopClient.Model
{
    [Serializable]
    public class DateTimeSortedFeedCollection : SortedList<DateTime, FeedClientModel>
    {
        private Dictionary<Guid, bool> loadedFeedGuid = new Dictionary<Guid, bool>();
        public DateTimeSortedFeedCollection Add(FeedClientModel model)
        {

            if (loadedFeedGuid.ContainsKey(model.FeedId))
            {
                if (this.ContainsKey(model.UpdateOn))
                {
                    this[model.UpdateOn].IsRead = model.IsRead;
                    this[model.UpdateOn].IsAvailable = model.IsAvailable;
                }
                else
                {
                    this.Remove(this.Values.First(feed => feed.FeedId == model.FeedId).UpdateOn);
                    this[model.UpdateOn] = model;
                }
            }
            else
            {
                this[model.UpdateOn] = model;
            }

            return this;
        }

        /// <summary>
        /// @Refactory this method should handle newitems and olditems after merge to let other model know the detail changes
        /// </summary>
        /// <param name="mergeWith"></param>
        /// <returns></returns>
        public DateTimeSortedFeedCollection Merge(IEnumerable<FeedClientModel> mergeWith)
        {
            foreach (var item in mergeWith)
            {
                this.Add(item);
            }
            return this;
        }
        public DateTimeSortedFeedCollection Merge(DateTimeSortedFeedCollection mergeWith)
        {
            this.Merge(mergeWith.Values);
            return this;
        }
        public DateTimeSortedFeedCollection(IEnumerable<FeedClientModel> values)
        {
            this.Merge(values);
        }
        public DateTimeSortedFeedCollection()
        { }
    }
}
